// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./myReplenishDetail.css");
	var databind = require("databind");
	var tools = require("tools");
	var creditId;
	var financeState;
	var flag = true;
	var pickupMethod;
	var deliveryMethod;
	var quantities = [];
	var inventoryIds = [];
	var bulkstockIds = [];
	var units = [];
	var caseQuantities = [];
	var deliveryDate;
	var deliveryAddress;
	var financeNumber;
	var valuationCal = require("valuationCal");
	var remark;
	var str1 = ['<span class="pickupTitle">添加新货箱：</span><a class="addInventory" style="float: right;" href="javascript:void(0);">添加</a>',
		'			<table>',
		'				<tr>',
		'					<th style="padding-left: 17px;">货物</th>',
		'					<th>重量(吨)</th>',
		'					<th>物流运单号</th>',
		'					<th>箱号</th>',
		'					<th>箱型尺寸</th>',
		'					<th>铅封号</th>',
		'					<th>托盘号</th>',
		'					<th>操作</th>',
		'				</tr>',
		'				<tr>',
		'					<td style="padding-left: 17px;">',
		'						<select name="bulkstockInfoId" class="bulkstockSelector"></select>',
		'					</td>',
		'					<td><input name="weight" type="text" /></td>',
		'					<td><input name="waybillNumber" type="text" /></td>',
		'					<td><input name="caseNumber" type="text" /></td>',
		'					<td><input name="caseSize" type="text" /></td>',
		'					<td><input name="facingSlip" type="text" /></td>',
		'					<td><input name="trayNumber" type="text" /></td>',
		'					<td></td>',
		'				</tr>',
		'			</table>'
	].join("");

	var str2 = ['<span class="pickupTitle">添加新货物：</span><a class="addBulkstock" style="float: right;" href="javascript:void(0);">添加</a>',
		'			<table>',
		'				<tr>',
		'					<th style="padding-left: 17px;">货物</th>',
		'					<th>生产厂家</th>',
		'					<th>产地</th>',
		'					<th>重量(吨)</th>',
		'					<th>估值</th>',
		'					<th>操作</th>',
		'				</tr>',
		'				<tr>',
		'					<td style="padding-left: 17px;">',
		'						<select name="bulkstockInfoId" class="bulkstockSelector"></select>',
		'					</td>',
		'					<td><input name="manufacturer" type="text" /></td>',
		'					<td><input name="originPlace" type="text" /></td>',
		'					<td><input name="weight" type="text" /></td>',
		'					<td><input type="hidden" name="bulkstockPrice" /><input class="bValuation" readonly="readonly" type="text" /><a class="valuation" href="javascript:void(0);">估值</a></td>',
		'					<td></td>',
		'				</tr>',
		'			</table>'
	].join("");

	var init = function(page) {
		creditId = page.params.creditId;
		router.init();
		tools.loadList("inquireFinanceCreditDetail.json", $("#myReplenishDetail"), $("#myReplenishDetailTpl"), {
			creditId: creditId
		}, function() {});
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}

		$(".pickupMethod").unbind("click").click(function() {
			if ($(".pickupMethod:checked").val() == 1) {
				$("#replenishInventory").html("");
				$("#replenishBulkstock").html("");
				$(".checkeds").attr("checked", true);
				for (var j = 0; j < $(".checkeds").length; j++) {
					var str = '<input name="inventoryIds" type="hidden" value="' + $(this).attr("inventoryId") + '" />';
					$("#inventoryIds").append(str);
					$("#inventoryIds").append('<input type="hidden" name="financeId" value="' + financeId + '" />');
				}
			} else if ($(".pickupMethod:checked").val() == 2) {
				$(".checkeds").attr("checked", false);
				$("#inventoryIds").html("");
				$("#replenishInventory").html("");
				$("#replenishBulkstock").html(str2);
				tools.loadBulkstockData($("#replenishBulkstock .bulkstockSelector"));

				$(".addBulkstock").unbind("click").click(function() {
					var s = '<tr><td style="padding-left: 17px;"><select name="bulkstockInfoId" class="bulkstockSelector"></select></td><td><input type="text" name="manufacturer" /></td><td><input type="text" name="originPlace" /></td><td><input type="text" name="weight" /></td><td><input readonly="readonly" type="text" class="bValuation" /><input type="hidden" name="bulkstockPrice" /><a class="valuation" href="javascript:void(0);">估值</a></td><td><a href="javascript:void(0);" class="delete">删除</a></td></tr>';
					$("#replenishBulkstock table").append(s);
					tools.loadBulkstockData($("#replenishBulkstock .bulkstockSelector:last"));
				});

				$("#replenishBulkstock").on("click", ".valuation", function() {
					valuationCal.showCal("货物估值", $(this).parents("tr").find(".bulkstockSelector").val(), "重量", $(this).parents("tr").find("input[name=weight]").val(), $(this).parents("tr").find(".bValuation"), $(this).parents("tr").find("input[name=bulkstockPrice]"), 1);
				});

				$("#replenishBulkstock").on("click", ".delete", function() {
					$(this).parents("tr").remove();
				});
			} else {
				$(".checkeds").attr("checked", false);
				$("#inventoryIds").html("");
				$("#replenishInventory").html(str1);
				$("#replenishBulkstock").html("");
				tools.loadBulkstockData($("#replenishInventory .bulkstockSelector"));

				$(".addInventory").unbind("click").click(function() {
					var s = '<tr><td style="padding-left: 17px;"><select name="bulkstockInfoId" class="bulkstockSelector"></select></td><td><input name="weight" type="text" /></td><td><input name="waybillNumber" type="text" /></td><td><input name="caseNumber" type="text" /></td><td><input name="caseSize" type="text" /></td><td><input name="facingSlip" type="text" /></td><td><input name="trayNumber" type="text" /></td><td><a href="javascript:void(0);" class="delete">删除</a></td></tr>';
					$("#replenishInventory table").append(s);
					tools.loadBulkstockData($("#replenishInventory .bulkstockSelector:last"));
				});

				$("#replenishInventory").on("click", ".delete", function() {
					$(this).parents("tr").remove();
				});
			}
		});

		$("#replenishSubmit").unbind("click").click(function() {
			if($("#myReplenishDetail input[name=pickupMethod]:checked").val()==2) {
				$("#myReplenishDetail").attr("action", "submitFinanceReplenishBulkstock");
			} else if($("#myReplenishDetail input[name=pickupMethod]:checked").val()==3) {
				$("#myReplenishDetail").attr("action", "submitFinanceReplenishContainer");	
			}
			databind.submitDom($("#myReplenishDetail"), function() {
				router.route("account/myReplenish.html");
			});
		});

	};

	return {
		init: init
	};
});